﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Waker.APIs.ZKT;
using Waker.Common;

namespace Waker.Cards
{
    public partial class FormStaff : Waker.Base.FormBaseToolBar
    {
        public FormStaff()
        {
            InitializeComponent();
            //base.InsertToolButton(3, true, barBtnFingerSetting);
            //base.InsertToolButton(4, false, barbtnFinger);
            //base.InsertToolButton(5, true, barBtnCardRegister);
            //base.InsertToolButton(6, true, barBtnLeave);
            this.Load += new EventHandler(FormStaff_Load);
            chkGrantCard.CheckState = CheckState.Indeterminate;
        }

        void FormStaff_Load(object sender, EventArgs e)
        {
            FormRefresh();
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FormRefresh();
        }
        public override void FormRefresh()
        {

            bool? isChek = null;
            if (chkGrantCard.CheckState != CheckState.Indeterminate)
                isChek = chkGrantCard.Checked;
            var bal = new BAL.BAL_Cards();
            Session.ListEmployeeAndCard = bal.GetAllEmployeeAndCard();
            var dt = bal.EmployeeSeach(txtEmp_Name.Text.Trim(), txtEmp_No.Text.Trim(), txtEmp_IDCard.Text.Trim(), isChek, false);
            this.gridControl1.DataSource = dt;
            if (gridView1.GroupCount > 0)
                gridView1.ExpandGroupRow(gridView1.FocusedRowHandle, false);

        }
        protected override void FormAdd()
        {
            var f = new FormStaffEdit(string.Empty);
            var res = f.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }
        protected override void FormEdit()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            var f = new FormStaffEdit(dr["Emp_ID"].ToString());
            var res = f.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks > 1)
            {
                FormEdit();
            }
        }
        protected override void FormDelete()
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null || dr["Emp_ID"] == DBNull.Value)
                return;
            if (UI.ShowWarningOKCancel(string.Format("确定删除员工[{0}]吗?", dr["Emp_Name"]), "删除提示") != DialogResult.OK)
                return;
            var res = new BAL.BAL_Cards().DeleteEmployee(dr["Emp_ID"].ToString());
            if (res)
            {
                UI.ShowInfo("删除成功");
                FormRefresh();
            }
            else
            {
                UI.ShowError("删除失败");
            }
        }
        void barbtnFinger_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            if (!Session.Finger_IsConneted)
            {
                var res = ZKTHelper.ZKTConnect();
                if (!res.IsSuccess)
                {
                    UI.ShowError("指纹机未连接,请连接后重试.");
                    return;
                }
            }
            //如果没有上传到指纹机 则上传后在录指纹
            if (dr["FingerId"] == DBNull.Value || Convert.ToInt32(dr["FingerId"]) < 1)
            {
                //var help = new ZKTHelper();
                var bal = new BAL.BAL_Cards();
                var fingerId = bal.GetNextFingerId();
                var res = ZKTHelper.ZKTSetUserInfo(
                      dr["Emp_Name"].ToString(),
                      fingerId,
                      string.Empty,
                      0,
                      true);
                if (!res.IsSuccess)
                {
                    //当前用户还没有上传到指纹机,
                    UI.ShowError("上传用户到指纹机失败");
                    return;
                }
                if (!bal.UpdateEmployeeFingerId(
                    dr["Emp_ID"].ToString(),
                    fingerId
                    ))
                {
                    UI.ShowError("保存用户指纹机id失败");
                    return;
                }
            }
            var f = new FormStaffFinger(dr["Emp_ID"].ToString());
            f.ShowDialog();
            f.Dispose();
            FormRefresh();
        }

        private void barBtnFingerSetting_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                var f = new FormFingerSetting();
                f.ShowDialog();
            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
        }

        private void barBtnCardRegister_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            var f = new FormStaffCard(dr["Emp_ID"].ToString());
            var res = f.ShowDialog();
            if (res == DialogResult.OK)
                FormRefresh();
        }

        private void barBtnLeave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            if (!UI.ShowOKCancelReturnBool(string.Format("确定要将[{0}]设置为离职员工?", dr["Emp_Name"])))
            {
                return;
            }
            var isok = new BAL.BAL_Cards().SetEmployeeLeave(dr["Emp_ID"].ToString(), true);
            if (isok)
            {
                UI.ShowInfo("离职设置成功");
                FormRefresh();
            }
            else
                UI.ShowError("离职设置失败");
        }

        private void barBtnWages_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0)
                return;
            var dr = gridView1.GetFocusedDataRow();
            if (dr == null)
                return;
            if (dr["IsFingerAny"] == DBNull.Value || !Convert.ToBoolean(dr["IsFingerAny"]))
            {
                UI.ShowError("该员工还没有登记指纹,请先登记指纹后重试");
                return;
            }
            if (dr["FingerId"] == DBNull.Value || dr["IsUploadFinger"] == DBNull.Value || Convert.ToInt32(dr["FingerId"]) < 1 || !Convert.ToBoolean(dr["IsUploadFinger"]))
            {
                UI.ShowError("指纹机上还没有该员工的数据,请先同步数据后重试");
                return;
            }
            var f = new FormWagesAdd(
                dr["Dept_Name"].ToString(),
                dr["Emp_ID"].ToString(),
                Convert.ToInt32(dr["FingerId"]),
                dr["Emp_Name"].ToString(),
                dr["Emp_No"].ToString(),
                dr["Emp_IDCard"].ToString()
                );
            f.ShowDialog();
            f.Dispose();
        }
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barBtnImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var f = new FormStaffImport();
            f.ShowDialog();
            FormRefresh();
        }
    }
}
